package com.changgou.business.listener;


import okhttp3.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

@Component
public class AdListener {

    @Autowired
    private RestTemplate restTemplate;

    @RabbitListener(queues = "ad_update_queue")
    public void receiveMessage(String message){
        System.out.println("接收到的消息为："+message);
        String url="http://192.168.200.128/ad_update?position="+message;

        //restTemplate发送请求
        /*ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        if (responseEntity.getStatusCode().value()==200){
            System.out.println("请求成功："+responseEntity.getBody());
        }*/

        //发远程调用
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request=new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //请求失败
                e.printStackTrace();
            }

            //响应方法
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("请求成功："+response.message());
            }
        });
    }
}
